package com.cloud.weixin.project.controller;


import com.cloud.common.core.asserts.ApiAssert;
import com.cloud.common.core.enums.ErrorCodeEnum;
import com.cloud.common.core.model.response.ApiResponses;
import com.cloud.common.core.model.supers.SuperController;
import com.cloud.common.security.annotation.PreAuth;
import com.cloud.weixin.project.model.dto.Menu;
import com.cloud.weixin.project.service.IWxMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 微信自定义菜单 前端控制器
 * </p>
 *
 * @author Sam
 * @since 2021-04-26
 */
@Api(tags = {"微信自定义菜单"})
@RestController
@RequestMapping("/wxMenu")
public class WxMenuRestController extends SuperController {

    @Autowired
    private IWxMenuService menuService;

    @PreAuth
    @ApiOperation("查询自定义菜单")
    @GetMapping("/list")
    public ApiResponses<Menu> list() {
        Menu dtoList = menuService.getWxMenuButton();
        return success(dtoList);
    }

    @PreAuth
    @ApiOperation("保存并发布菜单")
    @PostMapping("/release")
    public ApiResponses<Void> saveAndRelease(@RequestBody Menu menu) {
        try {
            menuService.saveAndRelease(menu);
        } catch (WxErrorException e) {
            ApiAssert.failure(ErrorCodeEnum.INTERNAL_SERVER_ERROR.convert("发布失败"), e);
        }
        return success();
    }
}

